package com.yanyeori.framework.elasticjob.config;

import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.yanyeori.framework.elasticjob.configprop.JobConfigProps;
import com.yanyeori.framework.elasticjob.job.JobManage;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * ElasticJob自动配置
 *
 * @author chenkuan 2020/8/19
 */
@Configuration
@EnableConfigurationProperties({JobConfigProps.class})
public class ElasticJobAutoConfig {

    @Bean
    public ZookeeperConfiguration zookeeperConfiguration(JobConfigProps config) {
        ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(config.getZookeeperServer(), config.getNamespace());
        zkConfig.setBaseSleepTimeMilliseconds(config.getBaseSleepTimeMilliseconds());
        zkConfig.setMaxSleepTimeMilliseconds(config.getMaxSleepTimeMilliseconds());
        zkConfig.setMaxRetries(config.getMaxRetries());
        zkConfig.setSessionTimeoutMilliseconds(config.getSessionTimeoutMilliseconds());
        zkConfig.setConnectionTimeoutMilliseconds(config.getConnectionTimeoutMilliseconds());
        return zkConfig;
    }

    @Bean(initMethod = "init")
    public CoordinatorRegistryCenter zookeeperRegistryCenter(ZookeeperConfiguration config) {
        return new ZookeeperRegistryCenter(config);
    }

    @Bean
    public JobManage jobManage(CoordinatorRegistryCenter coordinatorRegistryCenter, JobConfigProps JobConfigProps) {
        return new JobManage(coordinatorRegistryCenter, JobConfigProps);
    }
}
